package searcher;

import indexer.ReverseIndex;
import indexer.SimpleQuerer;

import java.util.List;

import util.CommonFactory;
import util.cache.core.AbstractRecoverResult;
import util.cache.core.Cacher;
import util.cache.normal.NormalCacher;
import algorithm.text.wordsegment.WordSegmentor;

/**
 * 最简单的Searcher，几乎没有使用任何优化
 * @author aleck
 *
 */
public class NaiveSearcher implements ISearcher {
	public static final int WS_ALGORITHM = WordSegmentor.ALGORITHM_DYNAMIC;
	public static final int MAX_SENTENCE_LEN_FOR_SUMMARY = 120;
	private final ReverseIndex ri;
	private final WordSegmentor ws;
	private final Cacher<Request, HitSet> cacher = 
		new NormalCacher<Request, HitSet>("", null, 100);

	public NaiveSearcher(ReverseIndex index) {
		this.ri = index;
		ws = CommonFactory.getWordSegmentor();
	}

	@Override
	public HitSet getSearchResult(Request request) {
		AbstractRecoverResult<HitSet> recoverResult = cacher.recover(request);
		HitSet recover = null;
		if (recoverResult != null) {
			return recoverResult.getValue();
		} else {
			SimpleQuerer sq = new SimpleQuerer(ri);
			sq.simpleQuery(request.getParsedQuery());
			HitSet results = sq.getResults();
			cacher.cache(request, results);
			return results;
		}
	}
}
